package et.item.wands.foci;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import et.common.ElectricThaum;
import ic2.api.item.ElectricItem;
import ic2.api.item.IElectricItem;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.util.IIcon;
import net.minecraft.util.MovingObjectPosition;
import net.minecraft.world.World;
import thaumcraft.api.aspects.Aspect;
import thaumcraft.api.aspects.AspectList;
import thaumcraft.api.wands.FocusUpgradeType;
import thaumcraft.api.wands.ItemFocusBasic;
import thaumcraft.common.items.wands.ItemWandCasting;

public class ItemFocusCharge extends ItemFocusBasic {
    long soundDelay = 0L;
    IIcon depthIcon = null;

    public ItemFocusCharge() {
        super();
        this.setCreativeTab(ElectricThaum.tabET);
        setUnlocalizedName("focusCharge");
        setTextureName("et:focus_charge");
    }

    @SideOnly(Side.CLIENT)
    public void registerIcons(IIconRegister ir) {
        this.icon = ir.registerIcon("et:focus_charge");
        this.depthIcon = ir.registerIcon("et:focus_charge_depth");
    }

    public String getSortingHelper(ItemStack itemstack) {
        return "AF" + super.getSortingHelper(itemstack);
    }
    public IIcon getFocusDepthLayerIcon(ItemStack itemstack) {
        return this.depthIcon;
    }
    public int getFocusColor(ItemStack itemstack) {
        return 10466299;
    }

    public AspectList getVisCost(ItemStack itemstack) {
        return new AspectList().add(Aspect.ORDER,500).add(Aspect.FIRE,500);
    }

    public int getActivationCooldown(ItemStack focusstack) {
        return 0;
    }

    public boolean isVisCostPerTick(ItemStack itemstack) {
        return true;
    }

    public WandFocusAnimation getAnimation(ItemStack itemstack) {
        return WandFocusAnimation.WAVE;
    }

    @Override
    public ItemStack onFocusRightClick(ItemStack itemstack, World world, EntityPlayer p, MovingObjectPosition movingobjectposition) {
        ItemWandCasting wand = (ItemWandCasting)itemstack.getItem();
        if(!wand.consumeAllVis(itemstack, p, this.getVisCost(itemstack), false, false)) {
            p.stopUsingItem();
        } else {
            for(int i = 0; i < 27; ++i) {
                ItemStack stack = p.inventory.getStackInSlot(i);
                if(stack != null && (stack.getItem() instanceof IElectricItem)) {
                    IElectricItem electricItem = ((IElectricItem)stack.getItem());
                    if(ElectricItem.manager.getCharge(stack)<electricItem.getMaxCharge(stack)) {
                        if(!p.worldObj.isRemote && wand.consumeAllVis(itemstack, p, this.getVisCost(itemstack), true, false)) {
                            ElectricItem.manager.charge(stack, 90, electricItem.getTier(stack), true, false);
                        }
                    }
                }
            }
        }
        return itemstack;
    }
    public boolean canApplyUpgrade(ItemStack focusstack, EntityPlayer player, FocusUpgradeType type, int rank) {
        return false;
    }
}

